<!doctype html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">
<html>
<head>
<meta http-equiv="content-type" content="text/html; charset=iso-8859-1">
<meta http-equiv="content-style-type" content="text/css">
<link rel="stylesheet" type="text/css" href="style.css">
<title>DexGuard Gradle Build</title>
</head>
<body>

<script type="text/javascript" language="JavaScript">
<!--
if (window.self==window.top)
  document.write('<a target="_top" href="index.html#gradlebuild.html" class="largebutton">DexGuard index</a>')
//-->
</script>
<noscript>
<a target="_top" href="index.html#gradlebuild.html" class="largebutton">DexGuard index</a>
</noscript>

<h2>Gradle Build</h2>

<b>DexGuard</b> can be integrated in the Gradle build process of the Android
SDK Tools (version 0.12.1, for Gradle version 1.12).

<h3>Setting up the Gradle build</h3>

You can enable DexGuard in the Android Gradle build by modifying the
<code>build.gradle</code> file of your project:
<pre>
buildscript {
    repositories {
        <b>flatDir { dirs '/usr/local/DexGuard5.5/lib' }</b>
        mavenCentral()
    }
    dependencies {
        <b>classpath ':dexguard:'</b>
        classpath 'com.android.tools.build:gradle:0.12.1'
    }
}

<b>apply plugin: 'dexguard'</b>

android {
    .....
    buildTypes {
        debug {
            <b>proguardFile getDefaultDexGuardFile('dexguard-debug.pro')</b>
            <b>proguardFile 'dexguard-project.txt'</b>
            proguardFile 'proguard-project.txt'
        }
        release {
            <b>proguardFile getDefaultDexGuardFile('dexguard-release.pro')</b>
            <b>proguardFile 'dexguard-project.txt'</b>
            proguardFile 'proguard-project.txt'
        }
    }
}
</pre>

Please make sure the repository path in the build script is set correctly for
your system. The <code>'dexguard'</code> plugin replaces the
<code>'android'</code> plugin &mdash; it's an extension.
<p>

The standard Gradle build process will now use DexGuard's Gradle task. You'll
see this in the console output: the line <code>:dexguardRelease</code> in the
console replace all lines <code>:dexRelease</code>,
<code>:packageRelease</code>, and <code>:zipalignRelease</code>.
<p>

You'll most comonly apply DexGuard to final <b>application projects</b>. This
is the easiest and most effective approach, since it processes the combined
code in a single step.
<p>

However, you can also apply DexGuard to <b>library projects</b>, if the
library aar file or jar file is the final product:
<pre>
buildscript {
    repositories {
        <b>flatDir { dirs '/usr/local/DexGuard5.5/lib' }</b>
        mavenCentral()
    }
    dependencies {
        <b>classpath ':dexguard:'</b>
        classpath 'com.android.tools.build:gradle:0.12.1'
    }
}

<b>apply plugin: 'dexguard-library'</b>

android {
    .....
    buildTypes {
        debug {
            <b>runProguard false</b>
            <b>proguardFile getDefaultDexGuardFile('dexguard-library-debug.pro')</b>
            <b>proguardFile 'dexguard-project.txt'</b>
            proguardFile 'proguard-project.txt'
        }
        release {
            <b>runProguard true</b>
            <b>proguardFile getDefaultDexGuardFile('dexguard-library-release.pro')</b>
            <b>proguardFile 'dexguard-project.txt'</b>
            proguardFile 'proguard-project.txt'
        }
    }
}
</pre>

Note the <code>-library-</code> in the configuration file names.
<p>

You should take some care with class encryption, as discussed for the
<a href="usage.html#encryptclasses">class encryption option</a>.
Furthermore, tamper detection is only supported if the final application is
also built and packaged using DexGuard.
<p>

You can find examples of working projects in the directory <code>samples</code>.

<h3>Building</h3>

The procedure for building Android applications and libraries remains the same.
You can invoke <code>gradle</code> with the usual targets, such
as <code>assemble</code>, <code>build</code>, <code>install</code>, and
<code>connectedInstrumentTest</code>. For instance, to build the release
version of your application and install it on a connected device:
<pre>
gradle installRelease
</pre>
<p>

To build the release version of a library:
<pre>
gradle assembleRelease
</pre>
<p>

Debug builds use debug settings, without optimization or obfuscation. Release
builds use release settings, with full optimization and obfuscation.
Applications can optionally be signed. The entries in application archives
are always zip-aligned for efficiency.
<p>

<h3>Further tips</h3>

As before, you can specify some useful options and properties for your Gradle
build:
<ul class="spacious">
<li>If you want to get verbose output for the entire build process, including
    DexGuard, you can run <code>gradle</code> with a more verbose log level:
<pre>
gradle -info .....
</pre></li>
<li>If you want to sign your application, you can specify the necessary
information in the standard <code>signingConfigs</code> container in the
<code>build.gradle</code> file of your project. For example, with hard-coded
values:
<pre>
android {
    .....
    signingConfigs {
        mySignature {
            storeFile     file('/home/user/.android/debug.keystore')
            storePassword 'android'
            keyAlias      'androiddebugkey'
            keyPassword   'android'
        }
    }

    defaultConfig {
        signingConfig signingConfigs.mySignature
    }
}
</pre>
</li>
</ul>

<h3>Quick troubleshooting</h3>

If you are having problems running the Gradle build itself:
<ul>
<li><b>ClassNotFoundException, ClassCastException,</b>...: make sure that you
    are using the DexGuard plugin with the proper version of the Android
    plugin in your <code>build.gradle</code> file. Also make sure that you are
    using the right version of Gradle itself. The Android plugin is still
    evolving rapidly, so even minor version differences can cause
    compatibility problems. You can have a look at the samples for working
    <code>build.gradle</code> configurations. Feel free to check with us if
    you think the DexGuard plugin is incompatible with a recent Android
    plugin.</li>
<li>You can get more information about the build process with the Gradle
    option <code>--info</code>, and even more with the option
    <code>--debug</code>. You then also see DexGuard's log output, which can
    be made more verbose with the DexGuard option <code>-verbose</code>.</li>
</ul>
<p>
Please consult the more extensive
<a href="troubleshooting.html">troubleshooting</a> section if you encounter
other issues building or running your application.

<h3>Reverting to the traditional Gradle build</h3>

You can go back to the basic Gradle build of the Android SDK by removing the
changes from the <code>build.gradle</code> file of your project.

<hr />
<address>
Copyright &copy; 2002-2014
<a target="other" href="http://www.saikoa.com/">Saikoa BVBA</a>.
</address>
</body>
</html>
